package com.docman.controller;

import java.io.IOException;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.docman.dao.DocumentDAO;
import com.docman.dao.UserDAO;

@WebServlet("/DocumentRatingServlet")
public class RatingServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public RatingServlet() {
        super();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
			if(request.getParameter("idBox")!=null && request.getParameter("rate")!=null){
			String idBox = request.getParameter("idBox");
				if(Pattern.matches("[0-9]+", idBox) == true){
				//System.out.println("Rating Document");
				int DocId = Integer.parseInt(request.getParameter("idBox"));
				int rating = Integer.parseInt(request.getParameter("rate"));
				DocumentDAO.rateDocument(DocId, rating);
				}
				else {
				//System.out.println("Rating User");
				String userName =request.getParameter("idBox");
				int rating = Integer.parseInt(request.getParameter("rate"));
				UserDAO.rateUser(userName, rating);
				}
					
			}
			
		
	}

}
